package zjh_20230406;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class DemoRecive {
    public static void main(String[] args) {

            int length = 1024;
            DatagramPacket datagramPacket = new DatagramPacket(new byte[length], length);

        try {
            DatagramSocket datagramSocket = new DatagramSocket(8000);
            while (true){
                datagramSocket.receive(datagramPacket);
                String str = new String(datagramPacket.getData(),0,datagramPacket.getLength());
                System.out.println(datagramPacket.getAddress().getHostName() + "发送了" + str);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
